home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 98 / CD-ROM 98.iso / infantil / tuxmath / tuxmath-2001.09.07-win32-installer.exe / src / setup.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-09-07  |  11.8 KB  |  505 lines

  1. /*
  2.   setup.c
  3.  
  4.   For TuxMath
  5.   Contains some globals (screen surface, images, some option flags, etc.)
  6.   as well as the function to load data files (images, sounds, music)
  7.   and display a "Loading..." screen.
  8.  
  9.   by Bill Kendrick
  10.   bill@newbreedsoftware.com
  11.   http://www.newbreedsoftware.com/
  12.  
  13.  
  14.   Part of "Tux4Kids" Project
  15.   http://www.tux4kids.org/
  16.       
  17.   August 26, 2001 - September 6, 2001
  18. */
  19.  
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <SDL.h>
  25. #ifndef NOSOUND
  26. #include <SDL_mixer.h>
  27. #endif
  28. #include <SDL_image.h>
  29. #include "setup.h"
  30. #include "images.h"
  31. #include "sounds.h"
  32. #include "game.h"
  33.  
  34.  
  35. /* (These need to be 'extern'd in "setup.h") */
  36.  
  37. SDL_Surface * screen;
  38. SDL_Surface * images[NUM_IMAGES];
  39. #ifndef NOSOUND
  40. Mix_Chunk * sounds[NUM_SOUNDS];
  41. Mix_Music * musics[NUM_MUSICS];
  42. #endif
  43. int use_sound, fullscreen, use_bkgd, demo_mode, oper_override, use_keypad;
  44. int opers[NUM_OPERS];
  45.  
  46.  
  47. /* Local function prototypes: */
  48.  
  49. void seticon(void);
  50. void usage(int err, char * cmd);
  51.  
  52.  
  53. /* --- Set-up function! --- */
  54.  
  55. void setup(int argc, char * argv[])
  56. {
  57.   int i, j, found, total_files;
  58.   SDL_Rect dest;
  59.  
  60.  
  61.   /* Set default options: */
  62.  
  63.   use_sound = 1;
  64.   fullscreen = 0;
  65.   use_bkgd = 1;
  66.   demo_mode = 0;
  67.   use_keypad = 0;
  68.  
  69.   for (i = 0; i < NUM_OPERS; i++)
  70.   {
  71.     opers[i] = 1;
  72.   }
  73.  
  74.  
  75.   /* FIXME: Program should load options from disk */
  76.  
  77.  
  78.   /* See if operator settings are being overridden by command-line: */
  79.  
  80.   for (i = 1; i < argc; i++)
  81.   {
  82.     if (strcmp(argv[i], "--operator") == 0 ||
  83.         strcmp(argv[i], "-o") == 0)
  84.     {
  85.       oper_override = 1;
  86.     }
  87.   }
  88.  
  89.  
  90.   /* If operator settings are being overridden, clear them first: */
  91.  
  92.   if (oper_override)
  93.   {
  94.     for (i = 0; i < NUM_OPERS; i++)
  95.     {
  96.       opers[i] = 0;
  97.     }
  98.   }
  99.  
  100.  
  101.   /* Get options from the command line: */
  102.  
  103.   for (i = 1; i < argc; i++)
  104.   {
  105.     if (strcmp(argv[i], "--help") == 0 || strcmp(argv[i], "-h") == 0)
  106.     {
  107.       /* Display help message: */
  108.  
  109.       printf("\nTux, of Math Command\n\n"
  110.         "Use the number keys on the keyboard to answer math equations.\n"
  111.         "If you don't answer a comet's math equation before it hits\n"
  112.         "one of your cities, the city's shields will be destroyed.\n"
  113.         "If that city is hit by another comet, it is destroyed completely.\n"
  114.     "When you lose all of your cities, the game ends.\n\n");
  115.  
  116.       printf("Run the game with:\n"
  117.         "--nosound      - to disable sound/music\n"
  118.     "--nobackground - to disable background photos (for slower systems)\n"
  119.     "--fullscreen   - to run in fullscreen, if possible (vs. windowed)\n"
  120.         "--keypad       - to enable the on-sceen numeric keypad\n"
  121.     "--demo         - to run the program as a cycling demonstration\n"
  122.     "--operator OP  - to automatically play with particular operators\n"
  123.     "                 OP may be one of:\n");
  124.  
  125.       for (j = 0; j < NUM_OPERS; j++)
  126.       {
  127.         printf("                   \"%s\"\n", oper_opts[j]);
  128.       }
  129.  
  130.       printf("\n");
  131.       
  132.  
  133.       exit(0);
  134.     }
  135.     else if (strcmp(argv[i], "--copyright") == 0 ||
  136.          strcmp(argv[i], "-c") == 0)
  137.     {
  138.       printf(
  139.     "\n\"Tux, of Math Command\" version " VERSION ", Copyright (C) 2001 Bill Kendrick\n"
  140.         "This program is free software; you can redistribute it and/or\n"
  141.         "modify it under the terms of the GNU General Public License\n"
  142.         "as published by the Free Software Foundation.  See COPYING.txt\n"
  143.     "\n"
  144.     "This program is distributed in the hope that it will be useful,\n"
  145.     "but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
  146.     "MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"
  147.     "\n");
  148.  
  149.       exit(0);
  150.     }
  151.     else if (strcmp(argv[i], "--usage") == 0 ||
  152.          strcmp(argv[i], "-u") == 0)
  153.     {
  154.       /* Display (happy) usage: */
  155.         
  156.       usage(0, argv[0]);
  157.     }
  158.     else if (strcmp(argv[i], "--fullscreen") == 0 ||
  159.          strcmp(argv[i], "-f") == 0)
  160.     {
  161.       fullscreen = 1;
  162.     }
  163.     else if (strcmp(argv[i], "--nosound") == 0 ||
  164.          strcmp(argv[i], "-s") == 0 ||
  165.          strcmp(argv[i], "--quiet") == 0 ||
  166.          strcmp(argv[i], "-q") == 0)
  167.     {
  168.       use_sound = 0;
  169.     }
  170.     else if (strcmp(argv[i], "--version") == 0 ||
  171.          strcmp(argv[i], "-v") == 0)
  172.     {
  173.       printf("Tux, of Math Command (\"tuxmath\")\n"
  174.          "Version " VERSION "\n");
  175.       exit(0);
  176.     }
  177.     else if (strcmp(argv[i], "--nobackground") == 0 ||
  178.              strcmp(argv[i], "-b") == 0)
  179.     {
  180.       use_bkgd = 0;
  181.     }
  182.     else if (strcmp(argv[i], "--demo") == 0 ||
  183.          strcmp(argv[i], "-d") == 0)
  184.     {
  185.       demo_mode = 1;
  186.     }
  187.     else if (strcmp(argv[i], "--keypad") == 0 ||
  188.              strcmp(argv[i], "-k") == 0)
  189.     {
  190.       use_keypad = 1;
  191.     }
  192.     else if (strcmp(argv[i], "--operator") == 0 ||
  193.          strcmp(argv[i], "-o") == 0)
  194.     {
  195.       if (i >= argc - 1)
  196.       {
  197.     fprintf(stderr, "%s option requires an argument\n", argv[i]);
  198.     usage(1, argv[0]);
  199.       }
  200.      
  201.       found = 0; 
  202.       for (j = 0; j < NUM_OPERS; j++)
  203.       {
  204.     if (strcmp(argv[i + 1], oper_opts[j]) == 0)
  205.     {
  206.           found = 1;
  207.           opers[j] = 1;
  208.     }
  209.       }
  210.  
  211.       if (found == 0)
  212.       {
  213.     fprintf(stderr, "Unrecognized operator %s\n", argv[i + 1]);
  214.     usage(1, argv[0]);
  215.       }
  216.  
  217.       i++;
  218.     }
  219.     else
  220.     {
  221.       /* Display 'made' usage: */
  222.  
  223.       fprintf(stderr, "Unknown option: %s\n", argv[i]);
  224.       usage(1, argv[0]);
  225.     }
  226.   }
  227.  
  228.  
  229.   if (demo_mode && use_keypad)
  230.   {
  231.     fprintf(stderr, "No use for keypad in demo mode!\n");
  232.     use_keypad = 0;
  233.   }
  234.  
  235.   
  236.   /* Init SDL Video: */
  237.   
  238.   if (SDL_Init(SDL_INIT_VIDEO) < 0)
  239.     {
  240.       fprintf(stderr,
  241.           "\nError: I could not initialize video!\n"
  242.           "The Simple DirectMedia error that occured was:\n"
  243.           "%s\n\n", SDL_GetError());
  244.       exit(1);
  245.     }
  246.   
  247.   
  248.   /* Init SDL Audio: */
  249.  
  250. #ifndef NOSOUND
  251.   if (use_sound)
  252.     { 
  253.       if (SDL_Init(SDL_INIT_AUDIO) < 0)
  254.         {
  255.           fprintf(stderr,
  256.                 "\nWarning: I could not initialize audio!\n"
  257.               "The Simple DirectMedia error that occured was:\n"
  258.               "%s\n\n", SDL_GetError());
  259.       use_sound = 0;
  260.         }
  261.     }
  262.   
  263.   if (use_sound)
  264.     {
  265.       if (Mix_OpenAudio(44100, AUDIO_S16, 2, 256) < 0)
  266.         {
  267.           fprintf(stderr,
  268.               "\nWarning: I could not set up audio for 44100 Hz "
  269.               "16-bit stereo.\n"
  270.               "The Simple DirectMedia error that occured was:\n"
  271.               "%s\n\n", SDL_GetError());
  272.           use_sound = 0;
  273.         }
  274.     }
  275. #endif
  276.   
  277.  
  278.   if (fullscreen)
  279.   {
  280.     screen = SDL_SetVideoMode(640, 480, 16, SDL_FULLSCREEN | SDL_HWSURFACE);
  281.  
  282.     if (screen == NULL)
  283.     {
  284.       fprintf(stderr,
  285.               "\nWarning: I could not open the display in fullscreen mode.\n"
  286.           "The Simple DirectMedia error that occured was:\n"
  287.           "%s\n\n", SDL_GetError());
  288.       fullscreen = 0;
  289.     }
  290.   }
  291.  
  292.   if (!fullscreen)
  293.     screen = SDL_SetVideoMode(640, 480, 16, SDL_HWSURFACE);
  294.  
  295.   if (screen == NULL)
  296.   {
  297.     fprintf(stderr,
  298.             "\nError: I could not open the display.\n"
  299.         "The Simple DirectMedia error that occured was:\n"
  300.         "%s\n\n", SDL_GetError());
  301.     exit(1);
  302.   }
  303.   
  304.   seticon();
  305.   SDL_WM_SetCaption("Tux, of Math Command", "TuxMath");
  306.  
  307.  
  308.   if (use_sound)
  309.     total_files = NUM_IMAGES + NUM_SOUNDS + NUM_MUSICS;
  310.   else
  311.     total_files = NUM_IMAGES;
  312.   
  313.  
  314.   /* Load images: */
  315.  
  316.   for (i = 0; i < NUM_IMAGES; i++)
  317.   {
  318.     images[i] = IMG_Load(image_filenames[i]);
  319.     if (images[i] == NULL)
  320.       {
  321.     fprintf(stderr,
  322.         "\nError: I couldn't load a graphics file:\n"
  323.         "%s\n"
  324.         "The Simple DirectMedia error that occured was:\n"
  325.         "%s\n\n", image_filenames[i], SDL_GetError());
  326.     exit(1);
  327.       }
  328.  
  329.     
  330.     if (i == IMG_STANDBY)
  331.       {
  332.     dest.x = (screen->w - images[IMG_STANDBY]->w) / 2;
  333.     dest.y = screen->h - images[IMG_STANDBY]->h - 10;
  334.     dest.w = images[IMG_STANDBY]->w;
  335.     dest.h = images[IMG_STANDBY]->h;
  336.     
  337.     SDL_BlitSurface(images[IMG_STANDBY], NULL, screen, &dest);
  338.     SDL_Flip(screen);
  339.       }
  340.     else if (i == IMG_LOADING)
  341.       {
  342.     dest.x = (screen->w - images[IMG_LOADING]->w) / 2;
  343.     dest.y = 0;
  344.     dest.w = images[IMG_LOADING]->w;
  345.     dest.h = images[IMG_LOADING]->h;
  346.     
  347.     SDL_BlitSurface(images[IMG_LOADING], NULL, screen, &dest);
  348.     SDL_Flip(screen);
  349.       }
  350.     else if (i == IMG_TITLE)
  351.       {
  352.     dest.x = (screen->w - images[IMG_TITLE]->w) / 2;
  353.     dest.y = images[IMG_LOADING]->h;
  354.     dest.w = images[IMG_TITLE]->w;
  355.     dest.h = images[IMG_TITLE]->h;
  356.     
  357.     SDL_BlitSurface(images[IMG_TITLE], NULL, screen, &dest);
  358.     SDL_Flip(screen);
  359.       }
  360.     
  361.     
  362.     dest.x = 0;
  363.     dest.y = (screen->h) - 10;
  364.     dest.w = ((screen->w) * (i + 1)) / total_files;
  365.     dest.h = 10;
  366.     
  367.     SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 255, 0));
  368.     SDL_Flip(screen);
  369.   }
  370.  
  371.  
  372. #ifndef NOSOUND
  373.   if (use_sound)
  374.   {
  375.     for (i = 0; i < NUM_SOUNDS; i++)
  376.     {
  377.       sounds[i] = Mix_LoadWAV(sound_filenames[i]);
  378.  
  379.       if (sounds[i] == NULL)
  380.       {
  381.         fprintf(stderr,
  382.             "\nError: I couldn't load a sound file:\n"
  383.                 "%s\n"
  384.                 "The Simple DirectMedia error that occured was:\n"
  385.                 "%s\n\n", sound_filenames[i], SDL_GetError());
  386.         exit(1);
  387.       }
  388.       
  389.       dest.x = 0;
  390.       dest.y = (screen->h) - 10;
  391.       dest.w = ((screen->w) * (i + 1 + NUM_IMAGES)) / total_files;
  392.       dest.h = 10;
  393.  
  394.       SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 255, 0));
  395.       SDL_Flip(screen);
  396.     }
  397.  
  398.  
  399.     for (i = 0; i < NUM_MUSICS; i++)
  400.     {
  401.       musics[i] = Mix_LoadMUS(music_filenames[i]);
  402.  
  403.       if (musics[i] == NULL)
  404.       {
  405.         fprintf(stderr,
  406.             "\nError: I couldn't load a music file:\n"
  407.                 "%s\n"
  408.                 "The Simple DirectMedia error that occured was:\n"
  409.                 "%s\n\n", music_filenames[i], SDL_GetError());
  410.         exit(1);
  411.       }
  412.       
  413.       dest.x = 0;
  414.       dest.y = (screen->h) - 10;
  415.       dest.w = ((screen->w) * (i + 1 + NUM_IMAGES + NUM_SOUNDS)) / total_files;
  416.       dest.h = 10;
  417.  
  418.       SDL_FillRect(screen, &dest, SDL_MapRGB(screen->format, 0, 255, 0));
  419.       SDL_Flip(screen);
  420.     }
  421.   }
  422. #endif
  423.   
  424.  
  425.   for (i = images[IMG_LOADING]->h; i >= 0; i = i - 10)
  426.     {
  427.       SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
  428.  
  429.       dest.x = (screen->w - images[IMG_TITLE]->w) / 2;
  430.       dest.y = i;
  431.       dest.w = images[IMG_TITLE]->w;
  432.       dest.h = images[IMG_TITLE]->h;
  433.       
  434.       SDL_BlitSurface(images[IMG_TITLE], NULL, screen, &dest);
  435.       SDL_Flip(screen);
  436.       SDL_Delay(10);
  437.     }
  438. }
  439.  
  440.  
  441. /* Set the application's icon: */
  442.  
  443. void seticon(void)
  444. {
  445.   int masklen;
  446.   Uint8 * mask;
  447.   SDL_Surface * icon;
  448.   
  449.   
  450.   /* Load icon into a surface: */
  451.   
  452.   icon = IMG_Load(DATA_PREFIX "/images/icon.png");
  453.   if (icon == NULL)
  454.     {
  455.       fprintf(stderr,
  456.               "\nWarning: I could not load the icon image: %s\n"
  457.               "The Simple DirectMedia error that occured was:\n"
  458.               "%s\n\n", DATA_PREFIX "images/icon.png", SDL_GetError());
  459.       return;
  460.     }
  461.   
  462.   
  463.   /* Create mask: */
  464.   
  465.   masklen = (((icon -> w) + 7) / 8) * (icon -> h);
  466.   mask = malloc(masklen * sizeof(Uint8));
  467.   memset(mask, 0xFF, masklen);
  468.   
  469.   
  470.   /* Set icon: */
  471.   
  472.   SDL_WM_SetIcon(icon, mask);
  473.   
  474.   
  475.   /* Free icon surface & mask: */
  476.   
  477.   free(mask);
  478.   SDL_FreeSurface(icon);
  479.   
  480.   
  481.   /* Seed random-number generator: */
  482.   
  483.   srand(SDL_GetTicks());
  484. }
  485.  
  486.  
  487. void usage(int err, char * cmd)
  488. {
  489.   FILE * f;
  490.  
  491.   if (err == 0)
  492.     f = stdout;
  493.   else
  494.     f = stderr;
  495.  
  496.  
  497.   fprintf(f,
  498.    "\nUsage: %s {--help | --usage | --copyright}\n"
  499.    "       %s [--fullscreen] [--nosound] [--nobackground] [--demo] [--keypad]\n"
  500.    "          [--operator {add | subtract | multiply | divide} ...]\n"
  501.     "\n", cmd, cmd);
  502.  
  503.   exit (err);
  504. }
  505.